Eye Drops Analysis for NIRS and Pulse Ox

Writing a new notebook to analyze eye drops only


In [1]:
#the usual beginning
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from pandas import Series, DataFrame, concat

#define any string with 'C' as NaN. 'C' is an indicator for the Masimo as a low signal reading
def readD(val):
    if 'C' in val:
        return np.nan
    return val

ROP Subject Number. CHANGE THIS BLOCK


In [2]:
# MAKE SURE YOU CHANGE THIS!!!
#
#
#

BabyNumber = 'ROP018'

In [3]:
dfNIRSpre0 = pd.read_csv('/Users/John/Dropbox/LLU/ROP/NIRS/Clean/' + BabyNumber + 'NIRS.csv',
            parse_dates={'timestamp': ['Date',' Time']},
            index_col='timestamp',
            usecols=['Date', ' Time', ' Ch2 %StO2'],
            na_values=['0'])

dfPOpre = pd.read_csv('/Users/John/Dropbox/LLU/ROP/Pulse Ox/' + BabyNumber + 'PO.csv',
            parse_dates={'timestamp': ['Date','Time']},
            index_col='timestamp',
            usecols=['Date', 'Time', 'SpO2', 'PR', 'PI', 'Exceptions'],
            na_values=['0'],
            converters={'Exceptions':  readD}
            )
dfNIRSpre = dfNIRSpre0.rename(columns={' Ch2 %StO2': 'StO2'}) #rename NIRS column

#parse_dates tells the read_csv function to combine the date and time column 
#into one timestamp column and parse it as a timestamp.
#    pandas is smart enough to know how to parse a date in various formats

#index_col sets the timestamp column to be the index.

#usecols tells the read_csv function to select only the subset of the columns.
#na_values is used to turn 0 into NaN

#converters: readD is the dict that means any string with 'C' with be NaN (for PI)

In [4]:
#phone almost always equals Philips monitor
"""NIRS date/time is 2 mins and 10 seconds slower than phone. Have to correct for it."""
TCnirs = timedelta(minutes=2, seconds=10)
"""Pulse ox date/time is 1 mins and 32 seconds faster than phone. Have to correct for it."""
TCpo = timedelta(minutes=1, seconds=32)

In [5]:
#time correct NIRS dataframe 
dfNIRS = dfNIRSpre.set_index([dfNIRSpre.index-TCnirs])

#df.set_index(['STK_ID','RPT_Date'], inplace = True)
#inplace has to be false because otherwise it won't create a new dataframe and it'll return "None" instead

#time correct PO dataframe
dfPO = dfPOpre.set_index([dfPOpre.index+TCpo])

In [6]:
#Clean the dataframe to get rid of rows that have NaN for PI purposes
dfPO_clean = dfPO[dfPO.loc[:, ['PI', 'Exceptions']].apply(pd.notnull).all(1)]

In [7]:
#Combine two DataFrame objects and default to non-null values in frame calling the method. 
#Result index columns will be the union of the respective indexes and columns
df = dfNIRS.combine_first(dfPO_clean)

Date and Time of Baseline and Eye Drops


In [8]:
df_first = df.first_valid_index() #get the first number from index
Y = pd.to_datetime(df_first) #convert index to datetime
# Y = TIME DATA COLLECTION BEGAN / First data point on CSV

# SYNTAX: 
# datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])

W1 = datetime(2016, 1, 20, 7, 35)
# W = first eye drop start
X = datetime(2016, 1, 20, 8, 42)
# X = ROP Exam Started
Z = datetime(2016, 1, 20, 8, 46)
# Z = ROP Exam Ended

df_last = df.last_valid_index() #get the last number from index

Q = pd.to_datetime(df_last) 

# Q = TIME DATA COLLECTION ENDED / Last Data point on CSV

In [9]:
W2 = datetime(2016, 1, 20, 7, 35) 
#W2 = second eyedrops

W3 = datetime(2016, 1, 20, 7, 40)
#W3 = third eyedrops

Baseline Average Calculation


In [10]:
avg0NIRS = df.StO2[Y:W1].mean()
avg0PI = df.PI[Y:W1].mean()
avg0O2 = df.SpO2[Y:W1].mean()
avg0PR = df.PR[Y:W1].mean()

print 'Baseline Averages\n', 'NIRS :\t', avg0NIRS, '\nPI :\t',avg0PI, '\nSpO2 :\t',avg0O2,'\nPR :\t',avg0PR,
#df.std() for standard deviation


Baseline Averages
NIRS :	87.5329586101 
PI :	1.35690874794 
SpO2 :	99.3392288645 
PR :	165.025744831

First Eye Drop Avg Every 10 Sec For 5 Minutes


In [11]:
def perdeltadrop1(start, end, delta):
    rdrop1 = []
    curr = start
    while curr < end:
        rdrop1.append(curr)
        curr += delta
    return rdrop1

dfdrop1NI = df.StO2[W1:W1+timedelta(minutes=5)]   
dfdrop1PI = df.PI[W1:W1+timedelta(minutes=5)]
dfdrop1O2 = df.SpO2[W1:W1+timedelta(minutes=5)]
dfdrop1PR = df.PR[W1:W1+timedelta(minutes=5)]
windrop1 = timedelta(seconds=10)

rdrop1 = perdeltadrop1(W1, W1+timedelta(minutes=5), windrop1)

avgdrop1NI = Series(index = rdrop1, name = 'StO2 1st ED')
avgdrop1PI = Series(index = rdrop1, name = 'PI 1st ED')
avgdrop1O2 = Series(index = rdrop1, name = 'SpO2 1st ED')
avgdrop1PR = Series(index = rdrop1, name = 'PR 1st ED')

for i in rdrop1:
    avgdrop1NI[i] = dfdrop1NI[i:(i+windrop1)].mean()
    avgdrop1PI[i] = dfdrop1PI[i:(i+windrop1)].mean()
    avgdrop1O2[i] = dfdrop1O2[i:(i+windrop1)].mean()
    avgdrop1PR[i] = dfdrop1PR[i:(i+windrop1)].mean()
    
resultdrops1 = concat([avgdrop1NI, avgdrop1PI, avgdrop1O2, avgdrop1PR], axis=1, join='inner')
print resultdrops1


                     StO2 1st ED  PI 1st ED  SpO2 1st ED   PR 1st ED
2016-01-20 07:35:00         93.0   1.566667   100.000000  176.833333
2016-01-20 07:35:10         92.6   1.616667   100.000000  176.166667
2016-01-20 07:35:20         92.0   1.966667    99.666667  171.500000
2016-01-20 07:35:30         92.0   1.950000    99.000000  166.500000
2016-01-20 07:35:40         92.6   1.700000   100.000000  176.666667
2016-01-20 07:35:50         93.0   1.800000    99.666667  172.000000
2016-01-20 07:36:00         92.8   1.583333    99.666667  176.333333
2016-01-20 07:36:10         93.0   1.550000    99.833333  171.833333
2016-01-20 07:36:20         91.8   1.583333    99.666667  176.500000
2016-01-20 07:36:30         92.0   1.566667    99.833333  176.833333
2016-01-20 07:36:40         92.6   1.666667   100.000000  177.833333
2016-01-20 07:36:50         92.8   1.600000   100.000000  183.333333
2016-01-20 07:37:00         93.0   1.633333   100.000000  178.166667
2016-01-20 07:37:10         93.0   1.683333    99.833333  179.000000
2016-01-20 07:37:20         93.0   1.766667    99.833333  175.000000
2016-01-20 07:37:30         93.0   1.700000    99.833333  171.166667
2016-01-20 07:37:40         92.2   1.716667    99.833333  174.166667
2016-01-20 07:37:50         92.2   1.616667    98.666667  173.000000
2016-01-20 07:38:00         92.2   1.600000    99.000000  173.166667
2016-01-20 07:38:10         92.0   1.400000    97.500000  177.500000
2016-01-20 07:38:20         93.0   1.733333    99.000000  176.333333
2016-01-20 07:38:30         93.0   1.850000    98.666667  178.666667
2016-01-20 07:38:40         93.0   1.533333    99.833333  167.833333
2016-01-20 07:38:50         92.2   1.683333    99.500000  158.000000
2016-01-20 07:39:00         92.2   1.733333   100.000000  156.500000
2016-01-20 07:39:10         92.4   1.833333    99.500000  160.333333
2016-01-20 07:39:20         93.0   1.766667    99.833333  157.166667
2016-01-20 07:39:30         93.0   1.800000   100.000000  160.166667
2016-01-20 07:39:40         93.0   1.666667    99.166667  165.333333
2016-01-20 07:39:50         93.0   2.083333    98.166667  162.000000

In [12]:
import matplotlib.pyplot as plt
#have graphs in ipython notebook
%matplotlib inline

In [13]:
dfgraph = df.drop(['Exceptions'], 1) #remove Exceptions

In [17]:
plt.figure() #http://matplotlib.org/api/figure_api.html customize the image
dfgraph.plot(subplots=True, figsize=(6, 6), use_index=False)

x=0
#plt.title('NIRS')
#plt.xlabel('%StO2')
#plt.ylabel('Time')
#plt.xticks(np.arange(min(x), max(x)+1, 1.0))
    
plt.show()


<matplotlib.figure.Figure at 0x112d9b9d0>

In [ ]: